package com.example.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.example.demo.entity.Student;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.pojo.dto.DeleteDto;
import com.example.demo.pojo.dto.add.StudentAddDto;
import com.example.demo.pojo.dto.update.StudentUpdateDto;
import com.example.demo.pojo.vo.PageVo;
import com.example.demo.service.IStudentService;
import com.example.demo.pojo.query.StudentQuery;
import com.example.demo.pojo.vo.StudentVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 学员信息表 服务实现类
 * </p>
 *
 * @author tds
 * @since 2021-09-30
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    /**
    * 学员信息表的分页查询
    *
    * @param query
    * @return
    */
    @Override
    public PageVo<StudentVo> queryPage(StudentQuery query) {
        // 开启分页查询
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        List<StudentVo> vos = getBaseMapper().queryAll(query);

        // todo 其他业务逻辑

        PageInfo<StudentVo> pageinfo = new PageInfo<>(vos);
        return new PageVo(pageinfo);
    }

    /**
     * 新增学员信息
     *
     * @param addDto
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(StudentAddDto addDto) {
        // todo 其他业务逻辑
        Student addDo = new Student();
        BeanUtil.copyProperties(addDto, addDo);
        this.save(addDo);
    }

    /**
     * 修改学员信息
     *
     * @param updateDto
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(StudentUpdateDto updateDto) {
        // todo 其他业务逻辑
        Student updateDo = new Student();
        BeanUtil.copyProperties(updateDto, updateDo);
        this.updateById(updateDo);
    }

    /**
     * 删除多条学员数据
     *
     * @param deleteDto
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(DeleteDto deleteDto) {
        // todo 其他业务逻辑
        if (CollUtil.isEmpty(deleteDto.getIds())) {
            return;
        }
        this.removeByIds(deleteDto.getIds());
    }
}
